package com.my.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.databind.annotation.JsonAppend;
import com.my.domain.entity.Announcement;
import com.my.domain.entity.Systematic;
import com.my.domain.entity.User;
import com.my.domain.vo.DashBoardVO;
import com.my.domain.vo.UserVO;
import com.my.mapper.UserMapper;
import com.my.result.Result;
import com.my.service.AnnouncementService;
import com.my.service.SystematicService;
import com.my.service.UserService;
import com.my.utils.AccountProfile;
import com.my.utils.BeanCopyUtils;
import io.swagger.models.auth.In;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
/**
 * 用户表(User)表服务实现类
 *
 * @author makejava
 * @since 2022-04-11 09:32:37
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Resource
    UserMapper userMapper;

    @Autowired
    AnnouncementService announcementService;

    @Autowired
    SystematicService systematicService;
    @Autowired
    RedisTemplate redisTemplate;

    /*给dashboard返回数据*/
    @Cacheable(cacheNames = "getUser")
    @Override
    public Object getUser() {
//        //拿到当前登录的对象
        Subject subject = SecurityUtils.getSubject();
        AccountProfile currentUser = (AccountProfile)subject.getPrincipal();// 拿到AccountProfile对象
        User user = getById(currentUser.getId());
        DashBoardVO vo = BeanCopyUtils.copyBean(user,DashBoardVO.class);
        Long numPeople = new Long(count());
        //去重查询总数
        QueryWrapper<Announcement> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("distinct account_id");
//        System.out.println("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<执行了这个");
        int count = announcementService.count(queryWrapper);

        LambdaQueryWrapper<Systematic> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        /*从缓存中拿,没有就去数据库中拿*/
        Object o = redisTemplate.opsForValue().get("systematic:LookNumber");
        if(o==null){
            Systematic systematic=systematicService.getById(1l);//先默认系统数据库只存访问量
            vo.setSystematicNum(systematic.getSystematicNum());
            redisTemplate.opsForValue().set("systematic:LookNumber",systematic.getSystematicNum());
            System.out.println("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<访问了一次数据库<<<<<<<<<<<<<<<<<<<<<<");
        }else{
            Long num = Long.valueOf((Integer) o);
            vo.setSystematicNum(num);
        }


        vo.setAnnouncements(announcementService.getAnnouncemnetById());

        vo.setPeopleNum(numPeople);
        vo.setAccountNum((long) count);

        return vo;
    }

    @Cacheable(cacheNames = "queryUserByName",key = "#username")
    @Override
    public User queryUserByName(String username) {
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(User::getAccountName,username);
        User user = userMapper.selectOne(lambdaQueryWrapper);
        return user;
    }
}
